<?php
namespace Home\Controller;

use Think\Controller;

class CartController extends SignInController
{

    public function getCartInfo()
    {
        $cartList = parent::get('cart');
        if (empty($cartList)) {

            $this->ajaxError("您还没有选择商品！");
        } else {
            $memberid = parent::get('account')['id'];

            $member = M('member')->where("id = $memberid and isDelete = 2 and isDisable = 2")->find();
            $this->ajaxReturn(array("cart" => $cartList));
        }
    }

    //购物车删除商品
    public function delete()
    {
        $values = I("cart");
        $values = explode(",", $values);

        if (!empty($values)) {
            $cartList = parent::get('cart');
            foreach ($values as $value) {
                unset($cartList[$value]);
            }
            parent::set('cart', $cartList);

            $this->ajaxSuccess($cartList);
        }
    }

    //操作购物车
    public function donum()
    {
        $id = (int)I('id');
        $type = (int)I('type');
        if (empty($id) || empty($type)) {
            $this->ajaxError('参数错误！');
        }
        $cart = parent::get("cart");
        if (!array_key_exists($id, $cart)) $this->ajaxError("商品不存在，请刷新后重试！");

        if (1 == $type) {
            //+
            $cart[$id]['num']++;
            parent::set('cart', $cart);
        } else if (2 == $type) {
            //-
            if ($cart[$id]['num'] -1 <= 0) return false;
            $cart[$id]['num']--;
            parent::set('cart', $cart);
        } else {
            $this->ajaxError("类型未知！");
        }
        $this->ajaxSuccess($cart);
    }

    //购物车跳转确认订单
    public function toDocart()
    {
        $cart = I('cart');
        $cart = explode(",", $cart);

        $cartList = parent::get('cart');
        $payCartList = array();

        if (!empty($cart)) {
            for ($i = 0; $i < count($cart); $i = $i + 2) {
                $id = $cart[$i];
                $num = ceil($cart[$i + 1]);
                if ($num <= 0 || $num > 99999) {
                    //数量小于等于0的不存入订单
                    $cartList[$id]['num'] = 0;
                } else {
                    $cartList[$id]['num'] = $num;
                    $payCartList[$id] = $cartList[$id];
                }
            }
        }
        parent::set('paycart', $payCartList);

        $this->ajaxSuccess($payCartList);
    }

    //确认订单页面
    public function docart()
    {
        $this->display("docart");
    }

    //获取确认订单信息
    public function getDoCartInfo()
    {
        $cart = parent::get('paycart');

        if (empty($cart)) {
            $this->ajaxError("订单列表为空");
        }
        $memberid = parent::get('account')['id'];

        $member = M('member')->where("id = $memberid and isDelete = 2 and isDisable = 2")->find();
        $mid = $member['id'];

        //地址
        $memberAddress = M("address")
            ->alias('m')
            ->join("LEFT JOIN t_region ON m.regionid=t_region.id")
            ->where(array("memberId" => $mid, "isDefault" => 1, "isDelete" => 2))
            ->field("m.*,t_region.name_path")
            ->find();
        if (empty($memberAddress)) {
            $memberAddress['address'] = "请设置默认收货地址";
        }
        //计算付款
        $total = 0;
        // 计算总价
        foreach ($cart as $product) {
            $total += $product['price'] * $product['num'];
        }

        $this->ajaxSuccess(array('cart' => $cart, 'member' => $member, 'memberAddress' => $memberAddress, 'total'=>$total));
    }
}